<template>
  <div class="product">
    <!-- 1. 输入框 -->
    <div class="header-input">
      <el-input
        v-model="input"
        placeholder="请输入内容"
        @change="changeInfo"
      ></el-input>
      <el-button type="primary" plain>查詢</el-button>
      <el-button type="primary" plain @click="addList">添加</el-button>
    </div>
    <!-- 提示框 -->
    <Prompt ref="myDialog" :rowDataInfo="rowDataInfo" :title="title" />
    <!-- <Search /> -->
    <!-- 2.表格数据列表 -->
    <el-table
      :data="tableData"
      style="width: 100%"
      ref="myTable"
      @selection-change="selectChange"
    >
      <el-table-column type="selection" width="40"> </el-table-column>
      <el-table-column prop="id" label="商品ID" width="80"> </el-table-column>
      <el-table-column
        prop="title"
        label="商品名称"
        width="120"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column prop="price" label="商品价格" width="100">
      </el-table-column>
      <el-table-column prop="num" label="商品数量" width="100">
      </el-table-column>
      <el-table-column prop="category" label="规格类目"> </el-table-column>
      <el-table-column prop="sellPoint" label="商品卖点" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="descs" label="商品描述" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="image" label="商品图片" show-overflow-tooltip>
      </el-table-column>

      <el-table-column label="商品操作" width="150">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">
            编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 按钮，多选框 -->
    <div style="margin-top: 20px">
      <el-button @click="toggleSelection([tableData[1], tableData[2]])"
        >切换第二、第三行的选中状态</el-button
      >
      <el-button @click="toggleSelection()">取消选择</el-button>
      <el-button @click="deleteSelection()">批量删除</el-button>
    </div>
    <!--3.  分页器 -->
    <Pagination
      :pageSize="pageSize"
      :total="total"
      @changeCurrent="changeCurrent"
    />
  </div>
</template>

<script>
import Pagination from "../../components/Pagination/Pagination";
import Search from "./Search";
import Prompt from "./Prompt";
export default {
  components: { Pagination, Search, Prompt },
  data() {
    return {
      indexCurrent: "",
      outerVisible: "",
      tableData: [],
      total: 10,
      pageSize: 1,
      input: "",
      idArr: [], // id 数组
      rowDataInfo: {}, //当前行信息
      title: "添加商品",
    };
  },
  methods: {
    /**
     * 首页编辑按钮
     */
    handleEdit(index, rowData) {
      // console.log("编辑事件", index, rowData);
      this.$refs.myDialog.dialogVisible = true;   // 显示弹框
      /**
       * 获得数据  -- 当前行数据传递给弹框
       * this.rowDataInfo = rowData;
       * 在堆中重新创建一个对象,Object.assign({}, rowData);
       * 使与取消按钮的row所指向地址不同
       */
      this.rowDataInfo = Object.assign({}, rowData);
      this.title = "编辑商品";
    },
    /**
     * 首页删除按钮
     */
    handleDelete(index, rowData) {
      // console.log("首页删除事件",index, rowData);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$api
          .deleteItemById({
            id: rowData.id,
          })
          .then((res) => {
            // console.log(res);
            if (res.status === 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              // 更新数据
              this.http(this.indexCurrent);
            }
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      });
    },
    // 網絡請求數據
    http(page) {
      this.$api.productList({ page }).then((res) => {
        // console.log(res.data);
        if (res.status === 200) {
          this.tableData = res.data;
          this.total = res.total;
          this.pageSize = res.pageSize;
        }
      });
    },
    changeCurrent(val) {
      this.indexCurrent = val;
      this.http(val);
    },
    // 查询网络接口
    search(search) {
      this.$api.search({ search }).then((res) => {
        if (res.status == 200) {
          // console.log(res.result);
          this.tableData = res.result;
          this.total = res.result.length;
          this.pageSize = 8;
        } else {
          console.log("查無數據");
          this.tableData = [];
          this.total = 1;
          this.pageSize = 1;
        }
      });
    },
    /**
     *  取消按钮  --- 多选按钮
     */
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          // 切换行显示
          this.$refs.myTable.toggleRowSelection(row);
        });
      } else {
        // 取消按钮
        this.$refs.myTable.clearSelection();
      }
    },
    /**
     * 多选按钮--选择
     */
    selectChange(selection) {
      // console.log("selection", selection);
      let arr = [];
      selection.forEach((ele) => {
        arr.push(ele.id);
      });
      this.idArr = arr;
    },

    /**
     *  批量删除按钮  --- 多选按钮
     */
    deleteSelection() {
      this.$api
        .batchDelete({
          idArr: this.idArr,
        })
        .then((res) => {
          console.log("======", res);
          if (res.status === 200) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            // 更新数据
            this.http(this.indexCurrent);
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    changeInfo(search) {
      if (search) {
        this.search(search);
      } else {
        this.http(1);
      }
    },
    addList() {
      this.$refs.myDialog.dialogVisible = true;
    },
  },
  created() {
    this.http(1);
  },
};
</script>

<style lang="less" scoped>
.product {
  margin-left: 210px;
  .header-input {
    display: flex;
    padding: 5px;
    input {
      margin: 0 5px;
    }
    button {
      margin: 0 5px;
    }
  }
}
</style>